﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class HistoryUserDao : BaseDao<HistoryUser>
    {
        #region instance

        private static HistoryUserDao? instance;
        public static HistoryUserDao Instance => instance ??= new HistoryUserDao();

        public override string GetTableName()
        {
            return "";
        }
        #endregion

        public List<HistoryUser> GetHistoryUsers()
        {
            string filePath = Constant.HUserFilePath;
            string xml = FileHelper.Reader(filePath, Encoding.UTF8);
            return (List<HistoryUser>)XmlHelper.Deserialize(typeof(List<HistoryUser>), xml);
        }

        public void UpdateUserPwd(User user)
        {
            if (user == null)
            {
                return;
            }
            List<HistoryUser> hUsers = GetHistoryUsers();
            foreach (var item in hUsers)
            {
                if (item.Phone == user.phone)
                {
                    item.Pwd = user.password;
                }
            }
            if (hUsers != null || hUsers?.Count > 0)
            {
                SaveToFile(hUsers);
            }
        }

        public void RemoveUser(User user)
        {
            if (user == null)
            {
                return;
            }
            List<HistoryUser> hUsers = GetHistoryUsers();
            if (hUsers == null || hUsers.Count <= 0) { return; }
            foreach (var item in hUsers)
            {
                if (item.Phone == user.phone)
                {
                    hUsers.Remove(item);
                    break;
                }
            }
          
            SaveToFile(hUsers);            
        }

        public void SaveUser(User? user, bool IsAutoLogin, bool IsRemberPwd)
        {
            if (user == null)
            {
                return;
            }
            //加密保存
            var temp = EncryptHelper.Encrypto(user.password);
            var hostoryUser = new HistoryUser()
            {
                UserName = user.loginName,
                Phone = user.phone,
                Pwd = temp,
                IsAutoLogin = IsAutoLogin,
                IsRemberPwd = IsRemberPwd
            };    
            List<HistoryUser> hUsers;
            string path = Constant.TempPath;
            string filePath = Constant.HUserFilePath;
            if (FileHelper.FolderExistsCreater(path))
            {
                if (!FileHelper.Exists(filePath))
                {
                    try
                    {
                        FileHelper.CreateFile(filePath);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("保存失败,文件创建失败：" + filePath + " msg:" + ex.Message);
                        return;
                    }
                }
                hUsers = GetHistoryUsers();
                if (hUsers == null)
                {
                    hUsers = new List<HistoryUser>();
                    hUsers.Add(hostoryUser);
                }
                else
                {
                    for (int i = 0; i < hUsers.Count; i++)
                    {
                        if (hUsers[i].Phone.Equals(hostoryUser.Phone))
                        {
                            hUsers.RemoveAt(i);
                            break;
                        }
                    }
                    hUsers.Insert(0, hostoryUser);
                }
                SaveToFile(hUsers);
            }
            else
            {
                Console.WriteLine("保存失败！路径创建失败：" + path);
            }
        }

        private void SaveToFile(List<HistoryUser> hUsers)
        {
            if (hUsers == null || hUsers.Count <= 0)
            {
                return;
            }
            string connStrings = XmlHelper.Serialize(typeof(List<HistoryUser>), hUsers);
            try
            {
                FileHelper.Write(Constant.HUserFilePath, connStrings);
            }
            catch (Exception ex)
            {
                Console.WriteLine("保存" + "msg:" + ex.Message);
            }
        }

    }
}
